package stock_controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import stock_service.ItemService;
import stock_vo.CheckedContents;
import stock_vo.ItemVO;

public class ItemController extends MultiActionController{
	private ItemService itemService;
	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}
	public ModelAndView registForm(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		System.out.println("Item registForm controller");
		ModelAndView mv = new ModelAndView("ijsonView");
		return mv;
	}
	public ModelAndView insertItem(HttpServletRequest request,
			HttpServletResponse response, ItemVO vo) throws Exception{
		ModelAndView mv = new ModelAndView("ijsonView");
		System.out.println("insertItem controller");
		try{
			itemService.insertItem(vo);
			List list = itemService.selectItem();
			mv.addObject("list", list);
		}catch(SQLException se){
			se.printStackTrace();
		}
		return mv;
	}
	public ModelAndView deleteItem(HttpServletRequest request,
			HttpServletResponse response, CheckedContents contents) throws Exception{
		ModelAndView mv = new ModelAndView("ijsonView");
		System.out.println("deleteItem controller");
		String c[] = contents.getContents();
		System.out.println(c.length);
		try{
			for(int i=0 ; i<c.length ;i++){
				String item_code = c[i];
				System.out.println("item_code = "+item_code);
				itemService.deleteItem(item_code);
			}
			List list = itemService.selectItem();
			mv.addObject("list", list);
		}catch(SQLException se){
			se.printStackTrace();
		}
		return mv;
	}
	public ModelAndView selectItem(HttpServletRequest request,
			HttpServletResponse response, ItemVO vo) throws Exception{
		System.out.println("selectItem controller");
		String search = request.getParameter("search");
		String url = "";
		System.out.println(search);
		ModelAndView mv=new ModelAndView();
		try{
			List list = itemService.selectItem();
			mv.addObject("list", list);
			System.out.println(list.size());
			url="stock/request/item_insert_request.tiles";
		}catch(SQLException se){
			mv.addObject("info", se.getMessage());
			se.printStackTrace();
		}
		if(search!=null){
			url="itemPopup";
		}
		mv.setViewName(url);
		return mv;
	}
}